package com.ruoyi.team.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.customer.service.ICustomerService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.team.service.IContractService;
import com.ruoyi.team.service.ICustomerLinkmaneService;
import com.ruoyi.team.service.ICustomerTeamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.team.mapper.CustomerVisitDemoMapper;
import com.ruoyi.team.domain.CustomerVisitDemo;
import com.ruoyi.team.service.ICustomerVisitDemoService;

/**
 * 拜访记录Service业务层处理
 *
 * @author lijiayang
 * @date 2022-11-27
 */
@Service
public class CustomerVisitDemoServiceImpl extends ServiceImpl<CustomerVisitDemoMapper, CustomerVisitDemo> implements ICustomerVisitDemoService {

    @Autowired
    private ISysUserService userService;
    @Autowired
    private ICustomerTeamService customerService;
    @Autowired
    private ICustomerLinkmaneService linkmaneService;


    /**
     * 查询拜访记录列表
     *
     * @param customerVisitDemo 拜访记录
     * @return 拜访记录
     */
    @Override
    public List<CustomerVisitDemo> selectCustomerVisitDemoList(CustomerVisitDemo customerVisitDemo) {
        List<CustomerVisitDemo> customerVisitDemos = getBaseMapper().selectCustomerVisitDemoList(customerVisitDemo);
        for (CustomerVisitDemo visitDemo : customerVisitDemos) {
            // 根据 id 查询录入人,并设置 nickname
            visitDemo.setInputNickName(userService.selectUserById(visitDemo.getInputUser()).getNickName());
            // 根据 客户id 查询客户,并设置 customerName
            visitDemo.setCustomerName(customerService.getById(Long.valueOf(visitDemo.getCustomerId())).getCustomerName());
            // 根据 联系人id 查询联系人,并设置 linkmanName
            visitDemo.setLinkmanName(linkmaneService.getById(Long.valueOf(visitDemo.getLinkmanId())).getLinkman());
        }
        return customerVisitDemos;
    }

    @Override
    public boolean save(CustomerVisitDemo entity) {
        // 获取登录对象 设置为录入人
        // LoginUser loginUser = SecurityUtils.getLoginUser();
        entity.setInputUser(SecurityUtils.getLoginUser().getUserId());
        // 获取当前时间 设置为录入时间
        entity.setInputTime(new Date());
        int insert = getBaseMapper().insert(entity);
        return insert > 0;
    }

}
